package com.luxury;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class HomeWebActivity extends AppCompatActivity {
    private Context mContext = this;
    private String titles = "";
    private String urls = "";
    @BindView(R.id.activity_hometype1)
    LinearLayout activityHometype1;
    private String productId = "";
    private static final String TAG = "hometye1Activity";
    @BindView(R.id.wv_hometype1Activity)
    WebView wvHometype1Activity;
    @BindView(R.id.text_webactivity)
    TextView textWebactivity;
    @BindView(R.id.image_return)
    ImageView imageReturn;
    @BindView(R.id.image_share)
    ImageView imageShare;
    private List<String> list_itemType1Urls = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);//初始化ShareSDK


        setContentView(R.layout.activity_hometype1);
        ButterKnife.bind(this);

        String url = getIntent().getStringExtra("url");
        int tabIndex = getIntent().getIntExtra("tabIndex", 0);
        wvHometype1Activity.loadUrl(url);
        Log.i(TAG, "传过来的网址" + url);
        urls = url;
        //使用webView打开网页
//        webViewDetail.setWebViewClient(new WebViewClient());
        wvHometype1Activity.getSettings().setJavaScriptEnabled(true);
//        wvHometype1Activity.setWebViewClient(new WebViewClient());
        wvHometype1Activity.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i(TAG, "shouldOverrideUrlLoading:--------------------------- " + url);
                geturl(view, url);
                return true;
            }
        });

        wvHometype1Activity.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                textWebactivity.setText(title + "");
                titles = title;
            }
        });
        imageReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                finish();
            }
        });
        imageShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1.创建一键分享对象
                OnekeyShare share = new OnekeyShare();
                share.disableSSOWhenAuthorize();//关闭sso授权
                //设置一键分享的内容
                share.setText(titles + urls + "");
                share.show(mContext);
            }
        });
    }

    private void geturl(WebView view, String url) {
        //判断字符串类型 拼接网址
        if (url.equals("http://m.secoo.com/appActivity/160906.shtml")) {
            view.loadUrl(url);
        } else {
            int start = url.indexOf(":");
            String urltype = url.substring(0, start);
            int index = url.indexOf("?");
            if (urltype.equals("http")) {
                if ((url.substring(0, index) + "").equals("http://m.secoo.com/proDetail.html")) {
                    jumping(url);
                } else {
                    view.loadUrl(url);
                }
            } else if (urltype.equals("secoo")) {
                jumping(url);
            }
        }

    }

    private void jumping(String url) {
        int begin = url.indexOf("=") + 1;
        int end = url.indexOf("&");
        String productId = url.substring(begin,end);
        String urlString = "http://android.secoo.com/appservice/query_goods.action?" + "productId="+productId;
        String img = "http://pic11.secooimg.com/product/300/300/" +productId;
        Log.i(TAG, "shouldOverrideUrlLoading:--------------------------- " + url);
        Log.i(TAG, "商品详情json地址" + urlString);
        Intent intent = new Intent(this, ProductActivity.class);
        intent.putExtra("url", urlString);
        intent.putExtra("productId", productId);
        intent.putExtra("imgHead", img);
        startActivity(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
  /*          if (wvHometype1Activity.canGoBack()) {
                wvHometype1Activity.goBack();//返回网页的上一页
                return true;
            } else {*/
            finish();

          /*  }*/
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


/*
    @OnClick({R.id.image_return, R.id.image_share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_return:
                finish();
                break;
            case R.id.image_share:

                break;
        }
    }*/
}
